package com.my.chapter16;

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        Cat[] cats = new Cat[6];
        Random rand = new Random();
        for(int i=0;i<6;i++){
            cats[i] = new Cat();
        }
        Collections.shuffle(Arrays.asList(cats),rand);
        System.out.println("Before sort：" + Arrays.toString(cats));
        Arrays.sort(cats,Collections.reverseOrder());
        System.out.println("Reverse sort：" + Arrays.toString(cats));
    }
}

class Cat implements Comparable<Cat>{
    static int count = 0;
    private final int id = count++;
    public int getId(){ return id; }

    @Override
    public int compareTo(Cat o) {
        int oid = o.getId();
        return id<oid ? -1 : (id==oid ? 0 : 1);
    }

    @Override
    public String toString() {
        return "Cat " + id ;
    }
}

/*Output：
Before sort：[Cat 4, Cat 2, Cat 0, Cat 3, Cat 5, Cat 1]
Reverse sort：[Cat 5, Cat 4, Cat 3, Cat 2, Cat 1, Cat 0]
*/


